home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8397 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  50 lines

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Bizzare C++ bug...PLEASE CHECK IT OUT
  5. Date: 17 Feb 1996 18:29:53 GMT
  6. Organization: Borland International
  7. Message-ID: <4g56r1$ep5@druid.borland.com>
  8. References: <4fsns9$8ga3@flute.aix.calpoly.edu>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4fsns9$8ga3@flute.aix.calpoly.edu>, mporcell@flute.aix.calpoly.edu 
  15. says...
  16. >
  17. >Clearly the call to the overloaded operator<< (r2) at r4 will cause a crash
  18. >because it is a call from a base class constructor (B) to a virtual print 
  19. >function (r1) that does not get defined until the derived class C (r5).
  20.  
  21. Nope. Calling a virtual function in a constructor results in a call to the 
  22. version of the function defined for that class. It does not call a "function 
  23. that does not get defined until the derived class". It is perfectly legal, 
  24. perfectly safe, and almost certainly incorrect.
  25.  
  26. class A
  27. {
  28. public:
  29.     virtual void f();
  30. };
  31.  
  32. class B : public A
  33. {
  34. public:
  35.     B();
  36. };
  37.  
  38. class C : public B
  39. {
  40. public:
  41.     virtual void f();    // overrides A::f()
  42. };
  43.  
  44. B::B()
  45. {
  46.     f();    // calls A::f, despite existence of C::f
  47. }
  48.  
  49.  
  50.